import store from "@/store";
import { parseStrEmpty } from "@/utils/ruoyi";
import { orderBy } from "lodash";
import decimal from "@/utils/big-decimal";

// 监听页面聚焦事件
export function handleFocus(label, index, value) {
  const info = {
    focusInput: label,
    page: index,
    value: value
  };
  store.dispatch("reportForms/changeFocus", info);
}
// 处理报表空数据转横线
export function emptyConvert(form, status = false) {
  if (Object.keys(form).length === 0) {
    return {};
  }
  // 是否打印空表
  const printEmpty = store.state.reportForms.printEmpty;
  if (printEmpty) {
    return form;
  }
  const info = { ...form };
  for (const key in info) {
    info[key] = emptyConvertAll(info[key], status);
  }
  return info;
}
function emptyConvertAll(value, status) {
  if (Array.isArray(value) && value.length > 0) {
    value.forEach((item, index) => {
      value[index] = emptyConvertAll(item, status);
    });
  } else if (value !== null && typeof value === "object") {
    for (const key in value) {
      value[key] = emptyConvertAll(value[key], status);
    }
  } else if (value == (status ? null : "-") || value == (status ? null : "")) {
    value = status ? "-" : null;
  }
  return value;
}

// 拆分字符串后去重
export function uniqueStr(str, type) {
  if (str == null || str == "") return "";
  const arr = str.split(type);
  let uniqueArr = [...new Set(arr)];
  return uniqueArr.join(type);
}

// table处理排序(报告在前，记录在后，正序排列)
export function sortData(dataList) {
  let arr1 = [];
  let arr2 = [];
  dataList.forEach((item) => {
    if (item.label.includes("bg")) {
      arr1.push(item);
    } else {
      arr2.push(item);
    }
  });
  // array = orderBy(dataList, ["typeSort"], ["desc"]);
  // console.log(array);
  return [...arr1, ...arr2];
}
// 计算均值
export function calculateMeanvalue(data, precision = 2) {
  if (!data || data.length <= 0) {
    return null;
  }
  return decimal.divide(
    data.reduce((sum, value) => decimal.add(sum, value, precision), 0),
    data.length,
    precision
  );
}
// 计算一组数据的标准差
export function calculateStandardDeviation(data, precision = 2) {
  if (!data || data.length <= 0) {
    return "";
  }
  // 计算均值
  const mean = calculateMeanvalue(data, 8);
  // console.log("均值" + mean);
  // 计算每个数据点与均值的差的平方，然后求和
  const sumOfSquares = data.reduce((sum, value) => {
    const difference = decimal.subtract(value, mean, 8);
    return decimal.add(sum, decimal.multiply(difference, difference, 8), 8);
  }, 0);
  // console.log("和" + sumOfSquares);
  // 计算方差
  const variance = decimal.divide(sumOfSquares, data.length - 1, 8);
  // console.log("方差" + variance);
  // 计算标准差，即方差的平方根
  const standardDeviation = Math.sqrt(variance);
  // console.log("标准差" + standardDeviation);

  return decimal.round(standardDeviation, precision);
}
// 精确到0.5
export function precision0p5(data) {
  if (!data) {
    return "";
  }
  // 乘法
  let product = data * 0.5;
  // 取整
  let integerPart = Math.floor(product);
  // 取余
  let remainder = product % 1;
  // 余数和0.25比较
  remainder = remainder >= 0.25 ? 0.5 : 0;
  // 整数除以0.5 + 余数
  const num = integerPart / 0.5 + remainder;
  return num;
}

// 监听上下左右
export function handleKeyDown(event, rowIndex, colIndex, maxArr) {
  const keyCode = event.keyCode;
  // 阻止方向键左右的默认行为
  if ([37, , 38, 39, 40].includes(event.keyCode)) {
    event.preventDefault();
  }
  let nextRow = rowIndex;
  let nextColumn = colIndex;
  const maxRow = maxArr.length;
  const maxCol = maxArr[rowIndex];
  switch (keyCode) {
    case 37: // left
      if (colIndex > 0) nextColumn--;
      // 当col为0 row大于0时，取row-1，col最大值
      if (colIndex == 0 && rowIndex > 0) {
        nextRow--;
        nextColumn = maxArr[nextRow];
      }
      break;
    case 39: // right
      if (colIndex < maxCol) nextColumn++;
      // 当col等于最大值，row小于最大行时，取row+1，col为0
      if (colIndex == maxCol && rowIndex < maxRow) {
        nextRow++;
        nextColumn = 0;
      }
      break;
    case 38: // up
      if (rowIndex > 0) nextRow--;
      // 当前col大于将要聚焦的那一行的col最大值时，取新行最大值
      if (nextColumn > maxArr[nextRow]) {
        nextColumn = maxArr[nextRow];
      }
      break;
    case 40: // down
      if (rowIndex < maxRow) nextRow++;
      // 当前col大于将要聚焦的那一行的col最大值时，取新行最大值
      if (nextColumn > maxArr[nextRow]) {
        nextColumn = maxArr[nextRow];
      }
      break;
  }
  // return { nextRow, nextColumn };
  return `${nextRow}_${nextColumn}`;
}


// 监听粘贴事件处理数据
export function handlePaste(event, rowIndex, colIndex, nameAllArr, methodsAllArr, parameterAllArr, subscriptAllArr) {
  event.preventDefault(); // 获取粘贴的数据
  const pastedData = event.clipboardData.getData("text/plain"); // 处理粘贴的数据...
  const rows = pastedData.split("\r\n"); // 去除空数组
  let arrays = rows.filter((array) => array.length > 0);
  const cols = arrays.map((row) => row.split("\t"));
  // 行
  const nameRowArr = nameAllArr.slice(rowIndex, rowIndex + cols.length > nameAllArr.length ? nameAllArr.length : rowIndex + cols.length);
  const methodsRowArr = methodsAllArr.slice(rowIndex, rowIndex + cols.length > methodsAllArr.length ? methodsAllArr.length : rowIndex + cols.length);
  const parameterRowArr = parameterAllArr.slice(rowIndex, rowIndex + cols.length > parameterAllArr.length ? parameterAllArr.length : rowIndex + cols.length);
  const subscriptRowArr = subscriptAllArr.slice(rowIndex, rowIndex + cols.length > subscriptAllArr.length ? subscriptAllArr.length : rowIndex + cols.length);

  // 行内多少个
  var nameSetArr = [];
  for (let a = 0; a < nameRowArr.length; a++) {
    nameSetArr.push(nameRowArr[a].slice(colIndex, colIndex + cols[a].length > nameRowArr[a].length ? nameRowArr[a].length : colIndex + cols[a].length));
  }
  var methodsSetArr = [];
  for (let a = 0; a < methodsRowArr.length; a++) {
    methodsSetArr.push(methodsRowArr[a].slice(colIndex, colIndex + cols[a].length > methodsRowArr[a].length ? methodsRowArr[a].length : colIndex + cols[a].length));
  }
  var parameterSetArr = [];
  for (let a = 0; a < parameterRowArr.length; a++) {
    parameterSetArr.push(parameterRowArr[a].slice(colIndex, colIndex + cols[a].length > parameterRowArr[a].length ? parameterRowArr[a].length : colIndex + cols[a].length));
  }
  var subscriptSetArr = [];
  for (let a = 0; a < subscriptRowArr.length; a++) {
    subscriptSetArr.push(subscriptRowArr[a].slice(colIndex, colIndex + cols[a].length > subscriptRowArr[a].length ? subscriptRowArr[a].length : colIndex + cols[a].length));
  }

  return {
    cols: cols,
    nameSetArr: nameSetArr,
    methodsSetArr: methodsSetArr,
    parameterSetArr: parameterSetArr,
    subscriptSetArr: subscriptSetArr,
  };
}